package com.book.app.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.app.aop.SystemControllerLog;
import com.book.app.entity.Book;
import com.book.app.service.BookService;
import com.book.app.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "馆藏图书模块")
@RestController
@RequestMapping("book")
@CrossOrigin //前后端分离配置跨域访问
public class BookController  {
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    @ApiOperation("分页查询馆藏图书信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始数据",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页显示分页最大数据",required = true,example = "10"),
            @ApiImplicitParam(name = "bname",value = "图书名称查询",example = "《红岩》"),
            @ApiImplicitParam(name = "bnum",value = "图书编号查询",example = "BN1001"),
    })
    @SystemControllerLog(description ="分页查询馆藏图书信息")
    @GetMapping("/findPager")
    public CommonResult findPager(Integer page, Integer limit,
                                  String bnum, String bname){
        QueryWrapper<Book>queryWrapper=new QueryWrapper<>();//封装条件查询的对象
        //根据馆藏图书编号进行条件查询
        if(!"".equals(bnum)&&bnum!=null){
            queryWrapper.like(true,"bnum",bnum);
        }
        //根据馆藏图书名称进行条件查询
        if(!"".equals(bname)&&bname!=null){
            queryWrapper.like(true,"bname",bname);
        }
        Map<String,Object>  map=new HashMap<>();
        map.put("page",page);
        map.put("count",bookService.count(queryWrapper));
        map.put("data",bookService.findPager(page,limit,bnum,bname));
        return CommonResult.success(map);
    }

    @ApiOperation("批量删除馆藏图书信息")
    @SystemControllerLog(description ="批量删除馆藏图书信息")
    @PostMapping("/removeBatch")
    public  CommonResult removeBatch(@RequestParam("bids[]") List<Integer>bids){
        boolean flag =bookService.removeByIds(bids);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("根据ID删除馆藏图书信息")
    @SystemControllerLog(description ="根据ID删除馆藏图书信息")
    @PostMapping("/remove")
    public  CommonResult remove(@RequestParam("bid") Integer bid){
        boolean flag =bookService.removeById(bid);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("添加馆藏图书信息")
    @SystemControllerLog(description ="添加馆藏图书信息")
    @PostMapping("/save")
    public  CommonResult save(@RequestBody Book book){
        book.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        boolean flag= bookService.save(book);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("更新馆藏图书信息")
    @SystemControllerLog(description ="更新馆藏图书信息")
    @PostMapping("/update")
    public  CommonResult update(@RequestBody Book book){
        book.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date()));
        boolean flag= bookService.updateById(book);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("查询所有馆藏图书信息，初始化借阅页面下拉列表")
    @SystemControllerLog(description ="查询所有馆藏图书信息，初始化借阅页面下拉列表")
    @GetMapping("/findAll")
    public CommonResult findAll(){
        return  CommonResult.success(bookService.list());
    }
}

